import 'package:flutter/cupertino.dart';
import 'package:wch/helper/endian_help.dart';
import 'package:wch/utils/digital_utils.dart';

import '../utils/log_utils.dart';
//
//AIO 解码
//
class DecodeEntity {
  ///协议名
  String name = '';
  bool result = false;
  int validateResult = 0;  //-1 已绑定 0用户取消 1绑定成功  2绑定成功且已有网
  ///解析数据，字符串
  String? content;
  ///解析数据，整型
  int? value;
  ///一体机设备信息
  Map<String, dynamic> info = {};

}
class AIODecode {
///收到数据并解包
  static DecodeEntity decodeData(List<int> data) {

    DecodeEntity entity = DecodeEntity();

    assert(data.isNotEmpty);

    assert(data[0] == 0xee);

    switch(data[1]) {
      ///验签
      case 0x01:
        entity.name = 'auth';
        entity.validateResult = auth(data[5]);
        break;
      case 0x02:
        appAndAIOMatch(data[5]);
        break;
      case 0x03:
        entity.name = 'pasterConnected';
        entity.result = pasterAndAIOConnected(data[5]);
        break;
      case 0x04:
        entity.result = true;
        entity.name = 'aioBattery';
        entity.value = AIOPower(data[5]);
        break;
      case 0x05:
        entity.result = true;
        entity.name = 'pasterBattery';
        entity.value = pasterPower(data[5]);
        break;
      case 0x06:
        entity.name = 'pasterInfo';
        pasterInfo(data, entity);
        break;
        ///
      case 0x09:
        AIOStatus(data[5]);
        break;
      case 0x0a:
        entity.name = 'aioWifi';
        entity.result = AIOWifiConnected(data);
        break;
      case 0x0b:
        entity.name = 'aioInfo';
        AIOInfo(data, entity);
        break;
      case 0x0c:
        FSMMode(data[5]);
        break;
      case 0x0d:
        wakeupTime(data[5], data[6], data[7]);
        break;
      case 0x0e:
        uploadPendingRecord(data[5]);
        break;
      case 0x70:
        entity.name = 'setWifi';
        entity.result = setWifiInfo(data[5]);
        break;
      case 0x71:
        setInteveneMode(data[5]);
        break;
      case 0x72:
        setUserInfo(data[5]);
        break;
      case 0x73:
        setPasterStatus(data[5]);
        break;
      case 0x74:
        realData(data[5]);
        break;
      case 0x77:
        upgrade(data[5]);
        break;
      case 0xb0:
        AIOHasData(data[5]);
        break;
    }

    return entity;
  }

  ///验签结果
  static int auth(int cmd) {
    int result = 0;

    if (cmd == 0x66) {
      LogUtils.log("验签成功");
      result = 1;
    } else if (cmd == 0xff) {
      LogUtils.log("用户取消");
      result = 0;
    } else if (cmd == 0x55) {
      LogUtils.log("用户已被其他设备绑定");
      result = -1;
    }else if (cmd == 0x77) {
      LogUtils.log("验签成功，已配网");
      result = 2;
    }
    return result;
  }
  ///一体机与APP的配对状态
  static void appAndAIOMatch(int result) {
    LogUtils.log(result == 0x01 ? "已配对" : "未配对");
  }
  ///一体机与额贴的连接状态
  static bool pasterAndAIOConnected(int result) {
    LogUtils.log(result == 0x01 ? "已连接" : "未连接");
    return result == 1;
  }
  ///充电盒电量
  static int AIOPower(int result) {
    LogUtils.log("充电盒电量=$result");
    return result;
  }
  ///额贴电量
  static int pasterPower(int result) {
    LogUtils.log("额贴盒电量=$result");
    return result;
  }
  ///567
  ///一体机工作状态
  static void AIOStatus(int result) {
    LogUtils.log("一体机工作状态=$result");
  }
  ///充电盒 wifi 状态
  static bool AIOWifiConnected(List<int> result) {
    LogUtils.log("AIOWifiConnected");
    // List<int> wifiData = result.sublist(7, 64+7);
    // NativeUtils.nativeEndianListener(wifiData, 1, 1);
    //暂时不查询 wifi 名称信息
    if (result[6] == 0) {
      return true;
    } else {
      return false;
    }

  }
  ///状态机的工作模式
  static void FSMMode(int result) {
    LogUtils.log("状态机的工作模式=$result");
  }
  ///用户设置的唤醒时间
  static void wakeupTime(int hour, int minute, int second) {
    LogUtils.log("用户设置的唤醒时间=$hour:$minute:$second");
  }
  ///用户未上报的记录条数
  static void uploadPendingRecord(int result) {
    LogUtils.log("用户未上报的记录条数=$result");
  }
  ///设置工作模式
  static void setInteveneMode(int result) {
    if (result == 0x66) {
      LogUtils.log("设置工作模式成功");
    } else if (result == 0xff) {
      LogUtils.log("设置工作模式失败");
    } else if (result == 0xfe) {
      LogUtils.log("指令不支持");
    }
  }
  ///设置额贴工作状态
  static void setPasterStatus(int result) {
    if (result == 0x66) {
      LogUtils.log("设置额贴工作状态成功");
    } else if (result == 0xff) {
      LogUtils.log("设置额贴工作状态失败");
    } else if (result == 0xfe) {
      LogUtils.log("指令不支持");
    }
  }
  ///设置用户信息
  static void setUserInfo(int result) {
    if (result == 0x66) {
      LogUtils.log("设置用户信息成功");
    } else if (result == 0xff) {
      LogUtils.log("设置用户信息失败");
    } else if (result == 0xfe) {
      LogUtils.log("指令不支持");
    }
  }
  ///请求实时数据
  static void realData(int result) {
    LogUtils.log("请求实时数据=$result");
  }
  ///开始升级
  static void upgrade(int result) {
    if (result == 0x66) {
      LogUtils.log("开始升级成功");
    } else if (result == 0xff) {
      LogUtils.log("开始升级失败");
    } else if (result == 0xfe) {
      LogUtils.log("指令不支持");
    }
  }
  ///一体机是否有数据
  static void AIOHasData(int result) {
    LogUtils.log("一体机是否数据=$result");
  }
  ///充电盒基本信息
  static void AIOInfo(List<int> cmd, DecodeEntity entity) {
    Map<String, dynamic> result = {};
    LogUtils.log("充电盒基本信息=$cmd");
    if (cmd.last == 0x66) {
      LogUtils.log("查询充电盒基本信息成功");
      result['aio_status'] = true;///附加信息，代表已经连接上一体机
      result['sn'] = EndianUtils.byteToAsciiString(cmd, 5, 16).trim();
      result['name'] = EndianUtils.byteToAsciiString(cmd, 21, 24).trim();
      String version = EndianUtils.byteToAsciiString(cmd, 45, 4).trim();
      result['version'] = '${version.substring(1, 2)}.${version.substring(2, 3)}.${version.substring(3, 4)}';
      result['battery'] = cmd[49];
      result['wifi_open'] = cmd[50] == 1;
      result['wifi_connected'] = cmd[51] == 1;
      result['wifi_name'] = EndianUtils.byteToAsciiString(cmd, 52, 64).trim();
      result['workStatus'] = cmd[116] == 1;
      entity.info = result;
      entity.result = true;
    } else if (cmd.last == 0xff) {
      LogUtils.log("查询充电盒基本信息失败");
    } else if (cmd.last == 0xfe) {
      LogUtils.log("指令不支持");
    }
  }
  ///额贴基本信息
  static void pasterInfo(List<int> cmd, DecodeEntity entity) {
    LogUtils.log("额贴基本信息=$cmd");
    Map<String, dynamic> result = {};
    if (cmd.last == 0x66) {
      LogUtils.log("查询额贴基本信息成功");
      result['name'] = EndianUtils.byteToAsciiString(cmd, 5, 24).trim();
      result['sn'] = EndianUtils.byteToAsciiString(cmd, 35, 16).trim();
      result['version'] = EndianUtils.byteToAsciiString(cmd, 51, 4).trim();
      result['battery'] = cmd[55];
      result['status'] = cmd[56] == 1;
      result['signal'] = DigitalUtils.byteToInt(cmd[57]);
      entity.info = result;
      entity.result = true;
      LogUtils.log('result=$result');
    } else if (cmd.last == 0xff) {
      LogUtils.log("查询额贴基本信息失败");
    } else if (cmd.last == 0xfe) {
      LogUtils.log("指令不支持");
    }
  }
  ///设置 WiFi 信息
  static bool setWifiInfo(int cmd) {
    bool result = false;

    if (cmd == 0x66) {
      LogUtils.log("设置WiFi成功");
      result = true;
    } else if (cmd == 0xff) {
      LogUtils.log("设置WiFi失败");
    } else if (cmd == 0xfe) {
      LogUtils.log("指令不支持");
    }

    return result;
  }
}